package com.zzzzzz.oms.shipmentInfo;

import java.util.List;
import java.util.Map;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Repository;
import com.zzzzzz.plugins.jdbctemplate.BaseDao;
import com.zzzzzz.plugins.jdbctemplate.Finder;
import com.zzzzzz.plugins.jdbctemplate.DaoUtils;
import com.zzzzzz.utils.page.Page;
import com.zzzzzz.plugins.shiro.I;

/**
 * @author hing
 * @version 1.0.0
 */
@Repository
public class ShipmentInfoDao {
	
	@Resource
	private BaseDao baseDao;
	
	// SQL 增、删、查、改
	private final static String sql_add = "insert into t_shipmentInfo(platformId, cd, constractorId, vehicleId, driverId,descr,shipmentMethod, addDt, addBy, st) values(:platformId, :cd, :constractorId, :vehicleId, :driverId, :descr,:shipmentMethod, :addDt, :addBy, :st)";
	private final static String sql_updStByIds = "update t_shipmentInfo set st = :st, updDt = :updDt, updBy = :updBy where id in (:ids)";
	private final static String sql_upd = "update t_shipmentInfo set platformId=:platformId, cd=:cd, constractorId=:constractorId, vehicleId=:vehicleId, driverId=:driverId,  descr=:descr,shipmentMethod=:shipmentMethod, updDt=:updDt, updBy=:updBy, st=:st where id = :id";
	private final static String sql_findById = "select id, platformId, cd, constractorId, vehicleId, driverId,  descr,shipmentMethod, updDt, addDt, addBy, updBy, st from t_shipmentInfo where id = :id";
	
	// 增加
	public Long add(ShipmentInfo shipmentInfo){
		return baseDao.updateGetLongKey(sql_add, shipmentInfo);
	}
	
	// 停用数据
	public int updStByIds(List<Long> ids, Integer st, I i){
		Finder finder = new Finder(sql_updStByIds).setParam("ids", ids).setParam("st", st)
		.setParam("updDt", new Date()).setParam("updBy", i.getId());
		return baseDao.update(finder);
	}
	
	// 修改数据
	public int updById(ShipmentInfo shipmentInfo){
		return baseDao.update(sql_upd, shipmentInfo);
	}
	
	// 查找一条数据
	public ShipmentInfo findById(Long id){
		Finder finder = new Finder(sql_findById).setParam("id", id);
		return baseDao.findOne(finder, ShipmentInfo.class);
	}
	
	// 动态查询
	public List<ShipmentInfo> findListBy(Page page,Map<String, Object> ffMap,I i){
		Finder finder = new Finder("");
		finder.append(" select");
		finder.append("count(1) as tt");
		finder.append(" from t_shipmentInfo s inner join t_constractor c on c.id=s.constractorId inner join t_vehicle v on v.id= s.vehicleId"
				+ " left join t_driver d on d.id =s.driverId inner join sys_dict dt on dt.descr=s.shipmentMethod inner join t_vehicleType vt on vt.id=v.vehicleTypeId");
		finder.append(" where 1=1");
		finder.append("and s.platformId=:platformId").setParam("platformId",i.getPlatformId());
		if(DaoUtils.isNotNull(ffMap.get("cd"))){
		finder.append(" and s.cd = :cd").setParam("cd", ffMap.get("cd"));
		}
		if(DaoUtils.isNotNull(ffMap.get("st"))){
			finder.append(" and s.st = :st").setParam("st", ffMap.get("st"));
		}else{
			finder.append(" and s.st = 0");
		}
		finder.append("order by s.id desc");
		if (page != null) {
			Integer total = baseDao.findInt(finder);
			page.setTotal(total);
			finder.limit(page.getStart(), page.getOffset());
		}
		String sql = finder.getSql().replace("count(1) as tt","s.*,c.shortName as constractorName,v.cd as vehicleCd,vt.name as vehicleTypeName,v.volume,v.weight,dt.val as shipmentMethodName,d.name as driverName");
		finder.setSql(sql);
		List<ShipmentInfo> list = baseDao.findList(finder, ShipmentInfo.class);
				
		return list;
	}
}
